<?php
namespace app\console\controller;

use app\console\model\Feedback as FeedbackModel;
use think\Cache;
use think\Request;
use think\Url;
use think\Db;
use think\Controller;
use think\Exception;
use think\Response;
use think\Loader;
use think\Session;

class Feedback extends Controller
{

    /*
     * 列表
     */
    public function index()
    {
        // 获取参数
        $inquire = Request::instance()->param();

        // 搜索条件
        $condition['id'] = ['neq', '0'];
        if (!empty($inquire['title'])) $condition['content|contact'] = ['like', '%' . $inquire['title'] . '%'];
        if(!empty($inquire['type_id'])) $condition['type_id'] = ['eq',$inquire['type_id']];
        if (!empty($param['interval_timer'])) {
            $interval_timer = explode('/', $param['interval_timer']);
            $condition['create_time'] = ['between', [$interval_timer[0], $interval_timer[1]]];
        }
        // 读取反馈记录
        $data = FeedbackModel::where($condition)->order(['id' => 'desc'])->where('is_delete','1')->paginate(10, false, ['query' => $inquire]);
        $list = Db::name('feedback_type')->select();
        return $this->fetch('index', [
            'mate_title' => '反馈记录列表',
            'list' => $list,
            'data'       => $data
        ]);
    }

    /*
     * 反馈记录删除
     */
    public function destroy($id)
    {

        if (Request::instance()->isGet()) {
            Db::name('feedback')->where('id',$id)->update(['delete_time' => date('Y-m-d H:i:s',time()),'is_delete' => 2,'delete_id' => session('manage_id')]);
            $type_id = Db::name('feedback')->where('id',$id)->value('type_id');
            $title = Db::name('feedback_type')->where('id',$type_id)->value('title');
            if(is_null($title)) $title = '';
            Db::name('recycle_bin')->insert(['title' => $title,'type' =>'意见反馈','table' => 'feedback','table_id' => $id,'delete_id' => Session::get('manage_id'),'delete_time' => date('Y-m-d H:i:s',time())]);
            win('操作成功', Url::build('Feedback/index'));
        }

        fail('操作失败');

    }

    /*
     * 反馈记录编辑
     */
    public function update($id)
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 验证
            $validate = Loader::validate('Feedback');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = (new FeedbackModel())->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('Feedback/index'));
            }

            fail('操作失败');

        }

        // 读取数据
        $item = FeedbackModel::get($id);

        return $this->fetch('create', [
            "mate_title" => '反馈记录查看',
            'item'       => $item

        ]);
    }

    /*
     * 排列顺序
     */
    public function sorting()
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {
            FeedbackModel::where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('操作成功', Url::build('Feedback/index'));
    }

}
